package vn.com.pavibook.daoImpl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import vn.com.pavibook.dao.AudioBookDao;
import vn.com.pavibook.domain.model.AudioBook;
import vn.com.pavibook.domain.model.PagingInfo;
import vn.com.pavibook.domain.model.SearchResult;
import vn.com.pavibook.utils.DaoHelper;

@Repository
public class AudioBookDaoImpl extends DaoHelper<AudioBook> implements
		AudioBookDao {

	@SuppressWarnings("rawtypes")
	class AudioBookRowMapper implements RowMapper {

		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			AudioBook book = new AudioBook();
			book.setId(rs.getInt("id"));
			book.setTitle(rs.getString("title"));
			book.setPrice(rs.getDouble("price"));
			book.setDiscount(rs.getDouble("discount"));
			book.setDescription(rs.getString("description"));
			book.setUrlAudio(rs.getString("url_audio"));
			book.setImageUrl(rs.getString("image_url"));
			try {
				book.setPublisherId(rs.getInt("publisher_id"));
			} catch (Exception e) {
			}

			try {
				book.setActive(rs.getInt("active"));
			} catch (Exception e) {
			}
			try {
				book.setCreatedDate(rs.getDate("create_date"));
			} catch (Exception e) {
			}

			return book;
		}

	}

	@Override
	public void save(AudioBook audioBook) {
		StringBuilder sql = new StringBuilder();
		Object[] params;
		if(audioBook.getId() == null) {
			sql.append("INSERT INTO audio_book (id, title, description, url_audio, created_date, ")
			.append("price, discount, publisher_id, image_url, active) ").append(" VALUES (?, ?, ?, ?, ? , ?, ?, ?, ?, ?)");
			params = new Object[] { audioBook.getId(),
					audioBook.getTitle(), audioBook.getDescription(),
					audioBook.getUrlAudio(), audioBook.getCreatedDate(),
					audioBook.getPrice(), audioBook.getDiscount(),
					audioBook.getPublisherId(), audioBook.getImageUrl(),
					audioBook.getActive() };
		} else {
			sql.append("UPDATE audio_book SET title = ?, description = ?, url_audio = ?, ")
			.append(" created_date = ?, price = ?, discount = ?, image_url = ?, active = ? WHERE id = ?");
			params = new Object[] {audioBook.getTitle(), audioBook.getDescription(),
					audioBook.getUrlAudio(), audioBook.getCreatedDate(),
					audioBook.getPrice(), audioBook.getDiscount(),
					audioBook.getImageUrl(), audioBook.getActive(), audioBook.getId() };
		}
		insert(sql.toString(), params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getAudioBook(int publisher_id, PagingInfo pagingInfo) {
		String sql = "Select * from audio_book WHERE publisher_id = ?";
		Object[] params = new Object[]{publisher_id};
		return queryPaging(sql, params, new AudioBookRowMapper(), pagingInfo);
	}

	@Override
	public void delete(int id) {
		String sql = "Delete from audio_book where id = ?";
		Object[] params = new Object[]{id};
		super.delete(sql, params);
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getAudioBookByid(int id) {
		String sql = "Select * from audio_book where id = ?";
		Object[] params = new Object[]{id};
		return query(sql, params, new AudioBookRowMapper());
	}

	@SuppressWarnings("unchecked")
	@Override
	public SearchResult<AudioBook> getListAudioBookByUsername(int publisherId,
			PagingInfo pagingInfo, String keyword) {
		StringBuilder sql = new StringBuilder("SELECT * FROM audio_book WHERE publisher_id = ? ");
		if(keyword != null && keyword.length() > 0) {
			sql.append(" AND title LIKE '%").append(keyword).append("%'");
		}
		Object[] params = new Object[]{publisherId};
		return queryPaging(sql.toString(), params, new AudioBookRowMapper(), pagingInfo);
	}
	
}
